import os
import json
import codecs
import numpy as np

base_dir = "/data/ppocr/detDataset/"
labelme_dir = os.path.join(base_dir, "biaozhu")
final_dir = os.path.join(base_dir, "top1000/biaozhu")


for name in os.listdir(labelme_dir):
    json_data = json.load(open(os.path.join(labelme_dir, name), "r"))
    shapes = []
    for part_label in json_data["shapes"]:
        part_lines = []

        shapes.append({
            "points": part_label["points"],
            "shape_type": part_label["shape_type"],
        })

    with codecs.open(os.path.join(final_dir, name.split(".")[0] + ".txt"), "a", "utf8") as f_part:
        for _shape in shapes:
            points = np.array(_shape["points"], dtype=np.float32)
            line_str = ""
            if _shape["shape_type"] == "polygon":
                line_str = ",".join([str(int(idx)) for idx in np.array(points).reshape(-1).tolist()])
            elif _shape["shape_type"] == "rectangle":
                xmin = str(int(min(points[:, 0])))
                xmax = str(int(max(points[:, 0])))
                ymin = str(int(min(points[:, 1])))
                ymax = str(int(max(points[:, 1])))
                line_str += "{},{},{},{},{},{},{},{}".format(
                    xmin, ymin, xmax, ymin, xmax, ymax, xmin, ymax
                )
            else:
                continue
            f_part.write(line_str+"\n")
